home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-02-07 | 8.8 KB | 209 lines | [TEXT/MPS ] |
- UNIT UEmergMem;
-
- {-------------------------------------------------------------------------------
- #
- # Apple Macintosh Developer Technical Support
- #
- # Interfaces for the emergency memory routines
- #
- # Program: ProcDoggie
- # File: UEmergMem.p - Pascal Implementation
- #
- # by: Forrest Tanaka
- #
- # Copyright © 1988-1991 Apple Computer, Inc.
- # All rights reserved.
- #
- --------------------------------------------------------------------------------
- #
- # EmergMem contains routines to handle emergency memory situations. This is
- # used for Toolbox routines that either don’t check for memory-full errors, or
- # that call _SysErr when they can’t allocate the memory that they need. The
- # purpose of the routines in this unit is to make sure that these toolbox
- # routines always get the memory they need.
- #
- -------------------------------------------------------------------------------}
- {[j=20/57/1$] Pasmat Options}
-
-
- INTERFACE
-
-
- (*******************************************************************************
- * Used Units
- *******************************************************************************)
-
- USES
- (* Group 1 *)
- Types
- ,QuickDraw
-
- (* Group 2 *)
- ,Memory
- ,OSUtils;
-
-
- (*******************************************************************************
- * Constants
- *******************************************************************************)
-
- CONST
- kAllocApp = TRUE; {For NewPtrMargin/NewHandleMargin for app heap alloc}
- kAllocClr = TRUE; {For NewPtrMargin/NewHandleMargin to clear mem block}
-
-
- (*******************************************************************************
- * ConnectAppGZ - Connect the application grow zone proc
- *
- * This routine is called whenever this application’s simple grow-zone procedure
- * (see UEmergMem.inc1.p for the source for the grow-zone procedure) is to be
- * connected. From this point on, any requests for memory by this application or
- * the system invoke our grow-zone procedure if there isn’t enough memory to
- * satisfy the request.
- *******************************************************************************)
-
- PROCEDURE ConnectAppGZ;
-
-
- (*******************************************************************************
- * DisconnectAppGZ - Disconnect the application grow zone proc
- *
- * This routine is called whenever this application’s simple grow-zone procedure
- * (see EmergMem.inc1.p for the source for the grow-zone procedure) is to be
- * disconnected. From this point on, any requests for memory by this application
- * or the system return memFullErr if there isn’t enough memory to satisfy the
- * request.
- *******************************************************************************)
-
- PROCEDURE DisconnectAppGZ;
-
-
- (*******************************************************************************
- * InitEmergMem - Allocate emergency memory
- *
- * This is called at startup time to allocate the emergency memory block that’s
- * deallocated in the grow zone procedure (this application’s grow-zone procedure
- * is a privately-declared procedure defined in UEmergMem.inc1.p). InitEmergMem
- * also installs this application’s grow-zone proc.
- *
- * If there isn’t enough memory to allocate the block of emergency memory, then
- * a subsequent call to FailLowMemory(0) returns TRUE.
- *******************************************************************************)
-
- PROCEDURE InitEmergMem;
-
-
- (*******************************************************************************
- * NoEmergMem - Check to see if emergency memory is being used or not
- *
- * Before my application attempts to use more memory, I call this routine to
- * check if I'm already using my emergency memory. If so, then I’d better
- * prepare to die or get my emergency memory back.
- *******************************************************************************)
-
- FUNCTION NoEmergMem: Boolean;
-
-
- (*******************************************************************************
- * RecoverEmergMem - Recover emergency memory
- *
- * This is called from the event loop if NoEmergMem indicates that the emergency
- * memory was deallocated by this application’s grow-zone procedure. This
- * routine will attempt recover the emergency memory. If this fails, then some
- * usually some application options and commands are disabled until there is
- * enough free memory to enable them again.
- *******************************************************************************)
-
- PROCEDURE RecoverEmergMem;
-
-
- (*******************************************************************************
- * FailLowMemory - Is there enough free space in heap to allocate memory?
- *
- * FailLowMemory is called any time a potentially significant amount of non-
- * temporary memory is about to be allocated. It returns TRUE if there’s enough
- * free space in the heap to allocate the requested amount of memory and still
- * have a significant amount of free space left over, and if the emergency memory
- * isn’t being used. See UEmergMem.inc1.p for the definition of “significant
- * amount.” "memRequest" specifies the number of bytes that are about to be
- * allocated.
- *
- * This routine is also used even if the amount of memory about to be allocated
- * isn’t clear. In this case, it’s called after the significant amount of memory
- * is allocated and 0 is passed in memRequest. If FailLowMemory returns TRUE,
- * then there’s was enough memory for the requested amount and still leave 32K
- * free and the emergency memory allocated. If FailLowMemory returns FALSE, then
- * either there isn’t 32K free, or the emergency memory was deallocated by this
- * application’s grow-zone procedure, or both. This is actually the usual way
- * that I use this function, because I normally use it for calls to the Toolbox,
- * and there’s usually no reliable way to determine how much memory the Toolbox
- * is going to allocate.
- *******************************************************************************)
-
- FUNCTION FailLowMemory (memRequest: LongInt): Boolean;
-
-
-
- (*******************************************************************************
- * NewHandleMargin - Create a new handle without using emergency memory
- *
- * Many toolbox routines simply call SysErr when they run out of memory. That’s
- * not too cool, so I try to make certain that the memory they need is always
- * available by making sure that I never request so much memory that the toolbox
- * routines are in danger of running out of memory and calling SysErr. This is
- * achieved by calling NewHandleMargin instead of NewHandle any time a
- * relocatable memory block is desired. NewHandle returns memFullErr in MemErr
- * if there isn’t enough free contiguous space to satisfy the request and still
- * leave a significant amount of free memory.
- *
- * NewHandleMargin returns NIL if there isn’t enough memory to allocated a block
- * of the size specified by "requestedSize".
- *
- * If "appHeapAlloc" is kAppHeap, then the block of memory is allocated in the
- * application’s heap. If "appHeapAlloc" is NOT kAppHeap, then the block of
- * memory is allocated in the system heap.
- *
- * If "clearMem" is kAllocClr, then all the bytes in the block of memory are
- * cleared to zero. If NOT kAllocClr is passed, then none of the bytes in the
- * block of memory are touched after being allocated.
- *******************************************************************************)
-
- FUNCTION NewHandleMargin (requestedSize: Size;
- appHeapAlloc: Boolean;
- clearMem: Boolean): Handle;
-
-
- (*******************************************************************************
- * NewPtrMargin - Create a new pointer without using emergency memory
- *
- * Many toolbox routines simply call SysErr when they run out of memory. That’s
- * not too cool, so I try to make certain that the memory they need is always
- * available by making sure that I never request so much memory that the toolbox
- * routines are in danger of running out of memory and calling SysErr. This is
- * achieved by calling NewPtr instead of NewHandle any time a non-relocatable
- * memory block is desired. NewHandle returns memFullErr in MemErr if there
- * isn’t enough free contiguous space to satisfy the request and still leave a
- * significant amount of free memory.
- *
- * NewptrMargin returns NIL if there isn’t enough memory to allocated a block of
- * the size specified by "requestedSize".
- *
- * If "appHeapAlloc" is kAppHeap, then the block of memory is allocated in the
- * application’s heap. If "appHeapAlloc" is NOT kAppHeap, then the block of
- * memory is allocated in the system heap.
- *
- * If "clearMem" is kAllocClr, then all the bytes in the block of memory are
- * cleared to zero. If NOT kAllocClr is passed, then none of the bytes in the
- * block of memory are touched after being allocated.
- *******************************************************************************)
-
- FUNCTION NewPtrMargin (requestedSize: Size;
- appHeapAlloc: Boolean;
- clearMem: Boolean): Ptr;
-
-
- IMPLEMENTATION
-
- {$I UEmergMem.inc1.p}
-
- END.